package com.etrans.web.gps.business.systemProcessing.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.etrans.web.gps.base.controller.BaseController;
import com.etrans.web.gps.business.systemProcessing.domain.SystemProcessing;
import com.etrans.web.gps.business.systemProcessing.service.SystemProcessingService;
import com.etrans.web.gps.common.page.PageVo;
import com.etrans.web.gps.util.grid.FlexiGridUtil;

@Controller
@RequestMapping("/systemProcessing/SystemProcessing")
public class SystemProcessingController extends BaseController{
	
	@Autowired
	private SystemProcessingService systemProcessingService;
	/**
	 * 
	 * @return
	 */
	@RequestMapping("toSystemProcessing.html")
	public String toSystemProcessing(){
		return "views/business/systemProcessing/systemProcessing";
	}
	
	/**
	 * 查询列表
	 * @param paramsGrid
	 * @return
	 */
	@RequestMapping(value = "queryAllSystemProcessing.html")
	@ResponseBody
	public PageVo queryAllSystemProcessing(String paramsGrid) {
		PageVo page = new PageVo();
		try {
			// 参数赋值
			HashMap<String, Object> paramMap = FlexiGridUtil.parseJSONParam(paramsGrid).getWhereParam();
			List<HashMap<String, Object>>  list =  systemProcessingService.queryAllAlarmType();
			Map<String,Object> map = new HashMap<String,Object>();
			for (HashMap<String, Object> hashMap : list) {
				String id= hashMap.get("id").toString();
				String text= hashMap.get("text").toString();
				map.put(id, text);
			}
			//列表
			List<SystemProcessing> rows = systemProcessingService.queryAllSystemProcessing(paramMap);
			for (SystemProcessing vo : rows) {
				String[] alarmTypes = vo.getAlarm_type().split(",");
				String alarmName="";
				for (int i = 0; i < alarmTypes.length; i++) {
					if(i==alarmTypes.length-1){
						alarmName += map.get(alarmTypes[i]);
					}else{
						alarmName += map.get(alarmTypes[i])+",";
					}
				}
				vo.setAlarm_name(alarmName);
			}
			page.setRows(rows);
			page.setPage((int) paramMap.get("page"));
			page.setTotal(systemProcessingService.queryAllSystemProcessingCount(paramMap));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return page;
	}
	
	/**
	 * 查询报警类型
	 * @return
	 */
	@RequestMapping(value = "queryAllAlarmType.html")
	@ResponseBody
	public List<HashMap<String, Object>>  queryAllAlarmType() {
		List<HashMap<String, Object>>  list = new ArrayList<>();
		try {
			list = systemProcessingService.queryAllAlarmType();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}
	
	/**
	 * 保存
	 * @return
	 */
	@RequestMapping("saveHandleDetail.html")
	public @ResponseBody Map<String,Object> saveHandleDetail(){
		Map<String,Object> rmap = new HashMap<String,Object>();
		Map<String,Object> map = this.getReqParameters();
		if(systemProcessingService.saveHandleDetail(map)){
			rmap.put("result","success");
		}else {
			rmap.put("result","fail");
		};
		return rmap;
	}
	
	/**
	 * 根据id删除
	 * @param id
	 * @return
	 */
	@RequestMapping("delSystemProcessing.html")
	public @ResponseBody Map<String,Object> delSystemProcessing(Integer id){
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("id", id);
		if(systemProcessingService.deleteStemProcessingByID(map)){
			map.put("result","success");
		}else {
			map.put("result","fail");
		};
		return map;
	}
	/**
	 * 根据id修改
	 * @param id
	 * @return
	 */
	@RequestMapping("updateEnable.html")
	public @ResponseBody Map<String,Object> updateStemProcessingByID(Integer id,String notice_time){
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("id", id);
		map.put("enable", 1);
		map.put("notice_time", notice_time);
		if(systemProcessingService.updateStemProcessingByID(map)){
			map.put("result","success");
		}else {
			map.put("result","fail");
		};
		return map;
	}
	
}
